home *** CD-ROM | disk | FTP | other *** search
/ Java for 3D & VRML Worlds / Java for 3d and VRML Worlds.iso / world / shadow / shadow.java < prev    next >
Text File  |  1996-10-17  |  4KB  |  249 lines

  1. // "Motion Shadow Effect"
  2.  
  3. //     created by ask@krc.sony.co.jp (Masamichi zzzcat Asukai)
  4.  
  5. //
  6.  
  7. // Copyright(C) 1996 Sony Corporation. All rights reserved.
  8.  
  9. //
  10.  
  11.  
  12.  
  13. import vrml.*;
  14.  
  15. import vrml.node.*;
  16.  
  17. import vrml.field.*;
  18.  
  19.  
  20.  
  21. public class shadow extends Script {
  22.  
  23.  
  24.  
  25.     private Node light;
  26.  
  27.     private Node object;
  28.  
  29.     private Node shadow;
  30.  
  31.     private boolean shadow_length;
  32.  
  33.     private boolean shadow_length_and_depth;
  34.  
  35.  
  36.  
  37.     private SFVec3f lightTranslation;
  38.  
  39.     private SFVec3f objectTranslation;
  40.  
  41.     private SFVec3f objectScale;
  42.  
  43.     private SFRotation shadowRotation;
  44.  
  45.     private SFVec3f shadowScale;
  46.  
  47.     private SFFloat shadowTransparency;
  48.  
  49.  
  50.  
  51.     private float[] l_trans = new float[3];
  52.  
  53.     private float[] o_trans = new float[3];
  54.  
  55.     private float[] o_scale = new float[3];
  56.  
  57.  
  58.  
  59.     private float[] s_rot = {0.0f, 1.0f, 0.0f, 0.0f};
  60.  
  61.     private float[] s_scale = {1.0f, 1.0f, 1.0f};
  62.  
  63.     private float s_transparency;
  64.  
  65.  
  66.  
  67.     private float[] vec = new float[3];
  68.  
  69.     private float d;
  70.  
  71.  
  72.  
  73.  
  74.  
  75.     public void initialize() {
  76.  
  77.  
  78.  
  79.     // get field values of script node
  80.  
  81.     light = (Node)((SFNode)getField("light")).getValue();
  82.  
  83.     object = (Node)((SFNode)getField("object")).getValue();
  84.  
  85.     shadow = (Node)((SFNode)getField("shadow")).getValue();
  86.  
  87.     shadow_length = 
  88.  
  89.         (boolean)((SFBool)getField("shadow_length")).getValue();
  90.  
  91.     shadow_length_and_depth = 
  92.  
  93.         (boolean)((SFBool)getField("shadow_length_and_depth")).getValue();
  94.  
  95.  
  96.  
  97.     // get exposed fields
  98.  
  99.     lightTranslation = (SFVec3f)light.getExposedField("translation");
  100.  
  101.     objectTranslation = (SFVec3f)object.getExposedField("translation");
  102.  
  103.     objectScale = (SFVec3f)object.getExposedField("scale");
  104.  
  105.     shadowRotation = (SFRotation)shadow.getExposedField("rotation");
  106.  
  107.     shadowScale = (SFVec3f)shadow.getExposedField("scale");
  108.  
  109.     shadowTransparency = (SFFloat)shadow.getExposedField("transparency");
  110.  
  111.     }
  112.  
  113.  
  114.  
  115.     
  116.  
  117.     public void processEvent(Event e) {
  118.  
  119.     if (e.getName().equals("interval")) {
  120.  
  121. System.out.println(shadowTransparency);
  122.  
  123.  
  124.  
  125.         lightTranslation.getValue(l_trans);
  126.  
  127.         objectTranslation.getValue(o_trans);
  128.  
  129.         objectScale.getValue(o_scale);
  130.  
  131.  
  132.  
  133.         ////////////////////
  134.  
  135.         // SHADOW ROTATION
  136.  
  137.         ////////////////////
  138.  
  139.  
  140.  
  141.         // normalized light vector on X-Z plane
  142.  
  143.         vec[0] = o_trans[0] - l_trans[0];
  144.  
  145.         vec[2] = o_trans[2] - l_trans[2];
  146.  
  147.         d = (float)java.lang.Math.sqrt(vec[0]*vec[0] + vec[2]*vec[2]);
  148.  
  149.         vec[0] /= d;
  150.  
  151.         vec[2] /= d;
  152.  
  153.  
  154.  
  155.         // rotation of shadow
  156.  
  157.         if (vec[0] < 0.0) {
  158.  
  159.             s_rot[3] = (float)java.lang.Math.acos(-vec[2]);
  160.  
  161.         } else {
  162.  
  163.             s_rot[3] = -(float)java.lang.Math.acos(-vec[2]);
  164.  
  165.         }
  166.  
  167.  
  168.  
  169.         // set rotation of shadow
  170.  
  171.             shadowRotation.setValue(s_rot);
  172.  
  173.  
  174.  
  175.         ////////////////////
  176.  
  177.         // SHADOW LENGTH
  178.  
  179.         ////////////////////
  180.  
  181.  
  182.  
  183.         if (true == shadow_length || 
  184.  
  185.             true == shadow_length_and_depth) {
  186.  
  187.             // whether light height is higher than top of object
  188.  
  189.             if (l_trans[1] < o_scale[1]) {
  190.  
  191.                 s_scale[2] = 0.0f;
  192.  
  193.             } else {
  194.  
  195.                 s_scale[2] = d / (l_trans[1] - o_scale[1]);
  196.  
  197.             }
  198.  
  199.  
  200.  
  201.             // set length of shadow
  202.  
  203.                 shadowScale.setValue(s_scale);
  204.  
  205.         }
  206.  
  207.  
  208.  
  209.  
  210.  
  211.         ////////////////////
  212.  
  213.         // SHADOW DEPTH
  214.  
  215.         ////////////////////
  216.  
  217.  
  218.  
  219.         if (true == shadow_length_and_depth) {
  220.  
  221.             if (s_scale[2] < 1.0f) {
  222.  
  223.                 s_transparency = 0.0f;
  224.  
  225.             } else {
  226.  
  227.                 s_transparency = 1.0f / s_scale[2];
  228.  
  229. //                s_transparency = 1.0f / (s_scale[2] * s_scale[2]);
  230.  
  231.                 s_transparency = 1.0f - s_transparency;
  232.  
  233.             }
  234.  
  235.  
  236.  
  237.             // set depth of shadow
  238.  
  239.                 shadowTransparency.setValue(s_transparency);
  240.  
  241.         }
  242.  
  243.         }
  244.  
  245.     }
  246.  
  247. }
  248.  
  249.